package work.droit.gui.swing;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.wb.swt.layout.grouplayout.GroupLayout;
import org.eclipse.wb.swt.layout.grouplayout.LayoutStyle;

import work.droit.data.Definition;
import work.droit.gui.ApiController;
import work.util.data.io.ManipFile;

public class DefinitionFrame extends Shell
{
	private Group	grpNavigation;
	private Text	defDef;
	private Text	motDef;
	private Button	btnSauve;
	private Button	btnSuivant;
	private Button	btnPrecedent;
	private Group grpCatgorie;
	private Text categorieText;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[])
	{
		try
		{
			Display display = Display.getDefault();
			DefinitionFrame shell = new DefinitionFrame(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed())
			{
				if (!display.readAndDispatch())
				{
					display.sleep();
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void launch(Definition definition, boolean editable)
	{
		Display display = getDisplay();
		motDef.setEditable(editable);
		defDef.setEditable(editable);
		categorieText.setEditable(editable);
		btnSauve.setVisible(editable);
		loadDefinition(definition);
		this.open();
		this.layout();
		while (!this.isDisposed())
		{
			if (!display.readAndDispatch())
			{
				display.sleep();
			}
		}
	}

	private void loadDefinition(Definition definition)
	{
		motDef.setText((String) definition.getValue(Definition.mot));
		String catName = (String) definition.getValue(Definition.categorie);
		if (catName != null) categorieText.setText(catName);
		int id = (Integer) definition.getValue(Definition.id);
		String def = ManipFile.getContent(ApiController.pathDocument + id + "d.txt");
		defDef.setText(def);
		btnSauve.setData(definition);

		Definition nextDef = (Definition) definition.next(Definition.mot);
		if (nextDef != null)
		{
			String m = (String) nextDef.getValue(Definition.mot);
			btnSuivant.setToolTipText(m);
			btnSuivant.setData(nextDef);
			btnSuivant.setEnabled(true);
		}
		else
		{
			btnSuivant.setToolTipText("Suivant");
			btnSuivant.setEnabled(false);
		}

		Definition prevDef = (Definition) definition.previous(Definition.mot);
		if (prevDef != null)
		{
			String m = (String) prevDef.getValue(Definition.mot);
			btnPrecedent.setToolTipText(m);
			btnPrecedent.setData(prevDef);
			btnPrecedent.setEnabled(true);
		}
		else
		{
			btnPrecedent.setToolTipText("Pr\u00E9c\u00E9dent");
			btnPrecedent.setEnabled(false);
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public DefinitionFrame(Display display)
	{
		super(display, SWT.SHELL_TRIM);
		setBackground(ApiController.bgColor);
		setImage(SWTResourceManager.getImage(DefinitionFrame.class, "/work/droit/rc/images/network_16.png"));

		grpNavigation = new Group(this, SWT.NONE);
		grpNavigation.setText("Navigation");
		FontData fontData = grpNavigation.getFont().getFontData()[0];
		Font font = new Font(display, new FontData(fontData.getName(), fontData.getHeight(), SWT.ITALIC));
		grpNavigation.setFont(font);

		Group grpDfinition = new Group(this, SWT.NONE);
		grpDfinition.setText("D\u00E9finition");
		fontData = grpDfinition.getFont().getFontData()[0];
		font = new Font(display, new FontData(fontData.getName(), fontData.getHeight(), SWT.ITALIC));
		grpDfinition.setFont(font);

		Group group = new Group(this, SWT.NONE);
		
		grpCatgorie = new Group(this, SWT.NONE);
		grpCatgorie.setText("Cat\u00E9gorie");
		grpCatgorie.setFont(font);
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(GroupLayout.TRAILING)
				.add(GroupLayout.LEADING, groupLayout.createSequentialGroup()
					.addContainerGap()
					.add(groupLayout.createParallelGroup(GroupLayout.LEADING)
						.add(GroupLayout.TRAILING, group, GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
						.add(grpDfinition, GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
						.add(grpNavigation, GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE)
						.add(grpCatgorie, GroupLayout.DEFAULT_SIZE, 412, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(GroupLayout.LEADING)
				.add(groupLayout.createSequentialGroup()
					.addContainerGap()
					.add(grpNavigation, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.UNRELATED)
					.add(grpDfinition, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.RELATED)
					.add(grpCatgorie, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.RELATED)
					.add(group, GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
					.addContainerGap())
		);
		grpCatgorie.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		categorieText = new Text(grpCatgorie, SWT.BORDER | SWT.READ_ONLY);
		categorieText.setTextLimit(Definition.categorie.size());
		group.setLayout(new FillLayout(SWT.HORIZONTAL));

		defDef = new Text(group, SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		FillLayout fl_grpDfinition = new FillLayout(SWT.HORIZONTAL);
		fl_grpDfinition.marginHeight = 10;
		grpDfinition.setLayout(fl_grpDfinition);

		motDef = new Text(grpDfinition, SWT.BORDER | SWT.READ_ONLY);
		motDef.setTextLimit(Definition.mot.size());
		grpNavigation.setLayout(new FillLayout(SWT.HORIZONTAL));

		btnPrecedent = new Button(grpNavigation, SWT.NONE);
		btnPrecedent.setImage(SWTResourceManager.getImage(DefinitionFrame.class,
				"/work/droit/rc/images/backward_16.png"));
		btnPrecedent.setText("Pr\u00E9cedent");
		btnPrecedent.addSelectionListener(new SelectionAdapter()
		{
			@Override
			public void widgetSelected(SelectionEvent e)
			{
				Button b = (Button) e.getSource();
				if (b.getData() != null)
				{
					loadDefinition((Definition) b.getData());
				}
			}
		});

		btnSauve = new Button(grpNavigation, SWT.NONE);
		btnSauve.setImage(SWTResourceManager.getImage(DefinitionFrame.class, "/work/droit/rc/images/wand_16.png"));
		btnSauve.setText("Sauvegarder");
		btnSauve.addSelectionListener(new SelectionAdapter()
		{
			@Override
			public void widgetSelected(SelectionEvent e)
			{
				Button b = (Button) e.getSource();
				if (b.getData() != null)
				{
					Definition def = (Definition) b.getData();
					def.setChamp(Definition.categorie, categorieText.getText());
					if (motDef.getText().length() >= 1) 
					{
						def.setChamp(Definition.mot, motDef.getText());
						def.update();
					}
					int id = (Integer) def.getValue(Definition.id);
					ManipFile.saveContent(ApiController.pathDocument + id + "d.txt", defDef.getText());
				}
			}
		});

		btnSuivant = new Button(grpNavigation, SWT.NONE);
		btnSuivant.setImage(SWTResourceManager.getImage(DefinitionFrame.class,
				"/work/droit/rc/images/forward_16.png"));
		btnSuivant.setText("Suivant");
		btnSuivant.addSelectionListener(new SelectionAdapter()
		{
			@Override
			public void widgetSelected(SelectionEvent e)
			{
				Button b = (Button) e.getSource();
				if (b.getData() != null)
				{
					loadDefinition((Definition) b.getData());
				}
			}
		});

		setLayout(groupLayout);

		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents()
	{
		setText("D\u00E9finition");
		setSize(450, 477);
	}

	@Override
	protected void checkSubclass()
	{
		// Disable the check that prevents subclassing of SWT components
	}
}
